package com.million.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.million.commonutils.R;
import com.million.eduservice.entity.EduTeacher;
import com.million.eduservice.entity.vo.teacher.TeacherListVo;
import com.million.eduservice.entity.vo.teacher.TeacherQuery;
import com.million.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author 十麓
 * @since 2022-03-19
 */
@Api(tags = "讲师管理")  //description已过时，用tags
@RestController
@RequestMapping("/eduservice/edu-teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 查询全部讲师信息
     *
     * @return 返回list集合
     */
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher () {
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items", list);

    }

    @ApiOperation(value = "根据ID逻辑删除讲师")
    @DeleteMapping("{id}")
    public R deleteThById (
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable("id") String id) {
        return eduTeacherService.removeById(id) ? R.ok() : R.error();
    }

    @ApiOperation(value = "直接分页查询")
    @GetMapping("{page}/{limit}")
    public R pageList (
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable("page") Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable("limit") Long limit) {
        Page<EduTeacher> pageList = new Page<>(page, limit);
        eduTeacherService.page(pageList, null);
        return R.ok().data("page", pageList);
    }

    @ApiOperation(value = "分页条件查询")
    @PostMapping("/pageQuery/{page}/{limit}")
    public R pageQuery (@ApiParam(name = "page", value = "当前页码", required = true)
                        @PathVariable("page") Long page,
                        @ApiParam(name = "limit", value = "每页记录数", required = true)
                        @PathVariable("limit") Long limit,
                        @RequestBody TeacherQuery teacherQuery) {
        Page<EduTeacher> teacherPage = new Page<>(page, limit);
        eduTeacherService.pageQuery(teacherPage, teacherQuery);
        return R.ok().data("teacherPage", teacherPage);
    }

    @ApiOperation(value = "添加教师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        if(eduTeacher == null){
            return null;
        }
        boolean save = eduTeacherService.save(eduTeacher);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @ApiOperation(value = "通过id查询讲师")
    @GetMapping("selectTeacherById/{id}")
    public R selectTeacherById(@PathVariable("id") String id){
        EduTeacher byId = eduTeacherService.getById(id);
        if(byId == null){
            return R.error().message("无该讲师");
        }else{
            return R.ok().data("teacher",byId);
        }
    }

    @ApiOperation(value = "通过id修改讲师")
    @PostMapping("updateTeacherById")
    public R updateTeacherById(@RequestBody TeacherListVo teacherListVo){
        eduTeacherService.updateTeacherById(teacherListVo);
        return R.ok();

    }
}


